package cn.iocoder.yudao.module.train.service.wxUser;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.train.controller.admin.wxuser.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.WxUser.WxUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.train.dal.mysql.WxUser.WxUserMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.*;

/**
 * 微信用户 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WxUserServiceImpl implements WxUserService {

    @Resource
    private WxUserMapper wxUserMapper;

    @Override
    public Long createWxUser(WxUserSaveReqVO createReqVO) {
        // 插入
        WxUserDO wxUser = BeanUtils.toBean(createReqVO, WxUserDO.class);
        wxUserMapper.insert(wxUser);

        // 返回
        return wxUser.getId();
    }

    @Override
    public void updateWxUser(WxUserSaveReqVO updateReqVO) {
        // 校验存在
        validateWxUserExists(updateReqVO.getId());
        // 更新
        WxUserDO updateObj = BeanUtils.toBean(updateReqVO, WxUserDO.class);
        wxUserMapper.updateById(updateObj);
    }

    @Override
    public void updateWxUser(WxUserDO updateReqVO) {
        // 校验存在
        validateWxUserExists(updateReqVO.getId());
        // 更新
        wxUserMapper.updateById(updateReqVO);
    }

    @Override
    public void deleteWxUser(Long id) {
        // 校验存在
        validateWxUserExists(id);
        // 删除
        wxUserMapper.deleteById(id);
    }

    @Override
        public void deleteWxUserListByIds(List<Long> ids) {
        // 删除
        wxUserMapper.deleteByIds(ids);
        }


    private void validateWxUserExists(Long id) {
        if (wxUserMapper.selectById(id) == null) {
            throw exception(WX_USER_NOT_EXISTS);
        }
    }

    @Override
    public WxUserDO getWxUser(Long id) {
        return wxUserMapper.selectById(id);
    }

    @Override
    public WxUserDO getWxUserByOpenId(String openId) {
        return wxUserMapper.selectByOpenId(openId);
    }

    @Override
    public PageResult<WxUserDO> getWxUserPage(WxUserPageReqVO pageReqVO) {
        return wxUserMapper.selectPage(pageReqVO);
    }

}
